/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oimwrapper.miscadapters;

import Thor.API.Operations.tcProvisioningOperationsIntf;
import Thor.API.tcResultSet;
import Thor.API.tcUtilityFactory;
import com.thortech.util.logging.Logger;
import com.thortech.xl.dataaccess.tcDataProvider;
import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class ProvisioningOps  {
    
    private tcDataProvider database = null;
    private static final Logger logger = Logger.getLogger(ProvisioningOps.class.getName());

    public ProvisioningOps() {
    }

    
    public void dummyMethod()
    {
        
    }
    
    public void setDatabase(tcDataProvider database)
    {
        this.database = database;
    }
    
    public String markOpenTaskComplete(String taskName,String taskStatus,long processInstanceKey)
    {
        logger.debug("Mark Task Complete");
        List<String> toClose = new ArrayList<String>();
        
        tcProvisioningOperationsIntf provOps = null;
                
        try
        {
            provOps = 
                    (tcProvisioningOperationsIntf)tcUtilityFactory.getUtility(database, "Thor.API.Operations.tcProvisioningOperationsIntf");
            tcResultSet rs = provOps.getProcessDetail(processInstanceKey);
            
            String[] headers = null;
            
            if (rs.getRowCount() > 0)
            {
                headers = rs.getColumnNames();
            }
            for(int i=0;i<rs.getRowCount();i++)
            {
                rs.goToRow(i);
                String name = rs.getStringValue("Process Definition.Tasks.Task Name");
                String status = rs.getStringValue("Status.Category");
                
                if (taskName.equalsIgnoreCase(name) && taskStatus.equalsIgnoreCase(status))
                {
                    String taskKey = rs.getStringValue("Process Instance.Task Details.Key");
                    if (taskKey != null && taskKey.length() > 0)
                        toClose.add(taskKey);
                }
                //for(int c=0;c<headers.length;c++)
                //{
                //    System.out.println(headers[c] + ":" + rs.getStringValue(headers[c]));
                //}
                
            }
        }
        catch(Exception e)
        {
            logger.error("Error Getting Tasks Complete",e);
            return "FAILURE";
            
        }
        
        if (toClose.size() > 0)
        {
            logger.debug("Closing Tasks:" + toClose);
            long[] tasksToClose = new long[toClose.size()];
            for(int i=0;i<toClose.size();i++)
            {
                logger.debug("Closing Tasks:" + toClose.get(i));
                tasksToClose[i] = Long.parseLong(toClose.get(i));
            }
            try
            {
                provOps.setTasksCompletedManually(tasksToClose);
            }
            catch(Exception e)
            {
                logger.error("Error Closing tasks",e);
                return "FAILURE";
            }
        }
        
        return "SUCCESS";
    }
    
    

}
